<?php

namespace app\api\controller;

use app\api\validate\PayValidate;
use think\Controller;
use think\Request;
use app\api\service\Pay as PayService;
include_once ($_SERVER['DOCUMENT_ROOT'] . "/../extend/WxPay/WxPay.Api.php");


class Pay extends Common
{
    protected $beforeActionList = [
        'checkExclusiveScope' => ['only' => ['getPreOrder']]
    ];

    /**
     * 生成预订单ID
     * @param int $orderId
     * @return array
     * @throws \app\lib\exception\ThrowError
     */
    public function getPreOrder($orderId = 0)
    {
        (new PayValidate('getPreOrder'))->goCheck();
        return PayService::pay($orderId);
    }

    /**
     * 支付回调处理
     * https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_7&index=8
     */
    public function receviveNotify()
    {
        // 检测库存，是否超卖
        // 更新订单状态
        // 减去库存
        // 返回告诉微信已成功接收处理信息

        $config =  new \WxPayConfig();
        $notify = new \WxPayNotify();
        // 在WxNotify.php中重写了NotifyProcess方法
        $notify->Handle($config);
    }
}
